// *******************************************************************************************************
// # リスト検索画面
//   ・フッター検索ボタン
//   ・選択ボタン
// *******************************************************************************************************

(function(win, doc) {
	function checkSelectBtn() {
		var $searchBtn = $('#fixedSearchBtn'),
			$modalBtnParent = $('.mod_btn_append'),
			$modalBtn = $('.mod_btn_append a'),
			$copyright = $('#siteCopyright'),
			$searchInput = $('.box_expand_search_list input'),
			isCheck = false;

		$searchInput.each(function(i){
			if ($(this).prop('checked')) {
				isCheck = true;
				return false;
			};
		});
		if (isCheck) {
			$searchBtn.show();
			$copyright.css('margin-bottom', 70);
			$modalBtn.addClass('mod_btn_all_pink');
			$modalBtnParent.removeClass('mod_btn_append_off');
		} else {
			$searchBtn.hide();
			$copyright.css('margin-bottom', 0);
			$modalBtn.removeClass('mod_btn_all_pink');
			$modalBtnParent.addClass('mod_btn_append_off');
		};
	}


	/* 選択ボタン
	----------------------------------- */
	function setSelectBtn() {
		var $searchBox = $('.box_expand_search_list .box_expand_inner');

		$searchBox.each(function(i){
			var $title = $(this).find('.ttl_expand input'),
				$list  = $(this).find('.list_expand input');
			$title.on('click', function(e){
				if ($(this).prop('checked')) {
					$list.prop('checked', true);
				} else {
					$list.prop('checked', false);
				};
				checkSelectBtn();
			});
			$list.on('click', function(e){
				$title.prop('checked', false);
				checkSelectBtn();
			});
		});
	}
	
	/*********************************
	 *前ページ選択されたものをチェックさせる
	 *********************************/
	$('input[name^=this_params]').each(function(){
		if($(this).attr('name').indexOf('target_') > 0){
			var id = $(this).val();
			$('input[id=' + id + ']').prop('checked', true);
			checkSelectBtn();
		}
	});
	/*
	 * Submitメソッド
	 */
	function submit(action, method, values) {
 	   var form = $('<form/>', {
	        action: action,
	        method: method
	    });
	    $.each(values, function() {
	        form.append($('<input/>', {
	            type: 'hidden',
	            name: this.name,
	            value: this.value
	        }));    
	    });
	    form.appendTo('body').submit();
	}	
	
	/*
	 * 検索条件連携準備
	 */
	function prepareParams(){		
		var items=[];
		var form = document.forms[0];
		for (var i=0; i<form.length; i++){
			if (form[i].name.match(/^this_params\[(.+)\]\[(.+)\]$/, "$1[$2]")) {
				var name = form[i].name.replace(/^this_params\[(.+)\]\[(.+)\]$/, "$1[$2]");
				if(name.indexOf('target') < 0 && name.indexOf('search_type') < 0 
						&& name.indexOf('page') < 0 && name.indexOf('Area') < 0){
					items.push({name:name,value:form[i].value});
				}					
			} 
			else if (form[i].name.match(/^this_params\[(.+)\]$/, "$1")) {
				var name = form[i].name.replace(/^this_params\[(.+)\]$/, "$1");
				if(name.indexOf('target') < 0 && name.indexOf('search_type') < 0 
						&& name.indexOf('page') < 0 && name.indexOf('Area') < 0){
					items.push({name:name,value:form[i].value});
				}
			}			
		}
		return items;
	}
	
	/*
	 * リンククリックして→希望条件ページ遷移
	 */
	function bindLinkEvent(){
		$('.mod_check').each(function(){
			var city = $(this).attr('id');
			city = city.substring(0,2) + '-' + city;
			$(this).find('a').on('click', function(){
				
				var items=[];
				items.push({name:'search_type',value:'area'});
				items.push({name:'target_0',value:city});					
				items.push({name:'page',value:'config'||1});
				submit('/sp/rstobi/rstobi','POST',items);
				
			});
		})
		
		//市区町村複数選択
		$("a[name=lnkCitySearch]").click(function() {
			var items=[];
			items.push({name:'search_type',value:'area'});
			items.push({name:'page',value:'config'});
			var c = 0;
			$('.list_expand input').each(function(index){
				if ($(this).prop('checked')) {
					c++;
		            var area_id = $(this).attr("id");
					items.push({name:'target_'+c,
					    value:area_id.substr(0,2)+'-'+area_id});
				}
			});
			if (c > 0) submit('/sp/rstobi/rstobi','POST',items);
		});
		
		//市区町村変更ページ
		$('a[name=lnkCityChange]').on('click', function(){
			var items=prepareParams();
			items.push({name:'search_type',value:'area'});
			items.push({name:'page',value:'config'});
			var c = 0;
			$('.box_check input').each(function(index){
				if ($(this).prop('checked')) {
					c++;
		            var area_id = $(this).attr("id");
					items.push({name:'target_'+c, value:area_id});
				}
			});
			if (c > 0) submit('/sp/rstobi/rstobi','POST',items);
		});
	}	
	
	/* 実行
	----------------------------------- */
	$(function() {
		//init();
		setSelectBtn();
		checkSelectBtn();
		bindLinkEvent();
	});

})(this, document);
